package com.jl15988.pguard.atomic;

import lombok.Setter;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

/**
 * 带有回调的原子引用
 * 用于在set时执行回调
 *
 * @author Jalon
 * @since 2025/1/16 16:14
 **/
@Setter
public class AtomicReferenceH<V> {

    private final AtomicReference<V> atomicReference;

    private Consumer<V> consumer;

    public AtomicReferenceH(V o) {
        atomicReference = new AtomicReference<>(o);
    }

    public final void set(V newValue) {
        consumer.accept(newValue);
        atomicReference.set(newValue);
    }

    public final V get() {
        return atomicReference.get();
    }
}
